--[[
  size = {x1,y1,x2,y2}
  num = 0 ~ 9
  fontLib = 字库
]]
function RxNumber(size, num, fontLibTable)
  closeFw() -- 测试
  local numStr = tostring(num)
  local tab = fontLibTable.num[num + 1]
  -- RxLog("RxNumber tab="..tab[1])
  local ctn, x, y = RxFindText(numStr, numStr, tab, size, fontLibTable.deviationColor, false)
  RxTestSize(size) -- 测试
  return ctn, x, y
end

local function RxNumberSortTable(direction, numTable)
  if type(numTable) == 'number' then
    RxLog('RxNumberSortTable => numTable 为 number---'..tostring(type(numTable)))
    return ''
  end

  if type(numTable) ~= 'table' then
    RxLog('RxNumberSortTable => numTable 不是 table ---'..tostring(type(numTable)))
    return ''
  end

  RxLog('RxNumberSortTable => type ---'..tostring(type(numTable)))
  -- local len = table.maxn(numTable)
  -- RxLog('RxNumberSortTable => numTable 的 长度='..tostring(len))
  -- if len <= 1 then
  --   return numTable[1].value
  -- end

  local isLeft = direction == "left" and true or false

  table.sort(
    numTable,
    function(a, b)
      -- 报错：table必须是从1到n连续的，即中间不能有nil，否则会报错
      if isLeft then
        if b == nil then
          return false
        end
        return a.id > b.id
      else
        if b == nil then
          return false
        end
        return a.id < b.id
      end
    end
  )

  local result = ""
  for _, v in pairs(numTable) do
    result = result..v.value
  end
  return result
end

local function RxNumberSlideLoop(direction, loopSize, maxSize, fontLibTable)
  local color = fontLibTable.color
  local x1, x2 = maxSize[1], maxSize[3]
  local _newNum = {}

  local isLeft = direction == "left" and true or false

  local newLoopSize = loopSize
  local numwidth = 15

  local _index = 0
  local whileLoop = true
  while whileLoop do
    if isLeft then
      loopx1 = newLoopSize[1] - numwidth
      if (loopx1 <= x1 - 10) then
        RxLog('RxNumberSlideLoop -> left - break => loopx1='..loopx1)
        break
      end
      newLoopSize = {loopx1, newLoopSize[2], newLoopSize[3] - numwidth + 7, newLoopSize[4]}
    else
      loopx2 = newLoopSize[3] + numwidth
      if (loopx2 >= x2 + 10) then
        RxLog('RxNumberSlideLoop -> right - break => loopx2='..loopx2)
        break
      end
      newLoopSize = {newLoopSize[1] + numwidth - 4, newLoopSize[2], loopx2, newLoopSize[4]}
    end
    local num = 0
    ::next::
    RxLog('_____RxNumberSlideLoop -> num='..tostring(num)..' 方向='..direction)
    RxLog('_____RxNumberSlideLoop -> newLoopSize=> x1='..newLoopSize[1]..' y1='..newLoopSize[2]..' x2='..newLoopSize[3]..' y2='..newLoopSize[4])
    local rtn, x, y = RxNumber(newLoopSize, num, fontLibTable)
    if rtn == 1 then
      RxLog("------RxNumberSlideLoop有结果了 方向="..direction)
      _index = _index + 1
      tempNum = tostring(num)
      RxLog("------RxNumberSlideLoop--num="..num.." rtn="..rtn.." x="..x.." y="..y..' 方向='..direction..' 值='..tempNum)
      _newNum[_index] = {id = _index, localtion = x, value = tempNum, direction = direction}
      newLoopSize[1] = x
      newLoopSize[3] = x + 14
    elseif num + 1 <= 9 then
      num = num + 1
      goto next
    end
  end

  RxLog("RxNumberSlideLoop 数字 识别结果 _index=".._index.." 方向="..direction)
  if _index == 0 then
    return 0
  end

  RxLog("RxNumberSlideLoop 数字 识别结果 _newNum 方向="..direction..' type='..tostring(type(_newNum)))
  return _newNum
end

function RxNumberLoopTable(size, fontLibTable)
  -- mSleep(1000)
  -- keepScreen(true)
  local color = fontLibTable.color
  local x1, y1, x2, y2 = size[1], size[2], size[3], size[4]
  local mindX = math.floor((x2 - x1) / 2 + x1) + 6.5

  local _newNum = {}
  local resultNum = ""

  local numwidth = 6.5
  local _index = 0
  local loopSize = {mindX - numwidth, y1, mindX + numwidth, y2}
  for fonti = 0, 9, 1 do
    local rtn, x, y = RxNumber(loopSize, fonti, fontLibTable)
    if rtn == 1 then
      -- dialog("有结果了")
      _index = _index + 1
      resultNum = tostring(fonti)
      RxLog("--------fonti="..fonti .." rtn="..rtn.." x=".. x .." y=".. y..' 中间值='..resultNum)
      _newNum[_index] = {id = _index, localtion = x, value = resultNum, direction = "center"}
      loopSize[1] = x
      loopSize[3] = x + 8
      break
    end
  end

  RxLog("数字 识别结果 _index=".._index..' 中间值='..resultNum)
  if _index == 0 then
    -- keepScreen(false)
    return 0, 0
  end

  local leftNumTable = RxNumberSlideLoop("left", loopSize, size, fontLibTable)
  local rightNumTable = RxNumberSlideLoop("right", loopSize, size, fontLibTable)
  -- keepScreen(false)

  local leftNum = RxNumberSortTable("left", leftNumTable)
  local rightNum = RxNumberSortTable("right", rightNumTable)

  local lastNum = leftNum..resultNum..rightNum
  dialog("lastNum=>> leftNum="..leftNum.." mind="..resultNum.." rightNum="..rightNum, 100)
  return 1, tonumber(lastNum)
end

function RxNumberGameTop(size)
  return RxNumberLoopTable(size, FontGameTopNum)
  -- return RxNumberLoopTable(size, FontGameTopNum2)
end
